    <!DOCTYPE HTML>
    <html>
    <HEAD>
    <meta charset="UTF-8">
    <TITLE>文章列表</TITLE>
    </HEAD>

    <?php
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    //开启session
    session_start();
    
    //如果session为空，则退出
    if(!$_SESSION['username']){
     echo "<script>alert('登陆失败');
         history.go(-1);</script>";   
    }
    include_once('../DB.php');
    include_once('base-class.php');
    $DB =  new DB();
    
    //获取当前页码
    if($_GET["page"]){
        $page=intval($_GET["page"]);
    }else { 
        $page = 0;
    }

    //查询转盘信息
    $res_list = $DB->query("select * from essay");
    $count = mysql_num_rows($res_list);

    //如果有数据
    if($count){
    //每页显示记录数
       $page_num = 5;
       //如果无页码参数则为第一页
        if ($page == 0) $page = 1;
        //计算开始的记录序号
        $from_record = ($page - 1) * $page_num;
        //获取符合条件的数据
        $res_list=$DB->get_all("select * from essay order by id asc limit $from_record,$page_num");
        

        //分页函数
        $multi = multi($count, $page_num, $page, "essayList.php");

    }
    //获取操作信息
    $action=$_GET["action"];
    //是否删除
    if($action=="del")
    {
        //获取ID号
        $id=intval($_GET["id"]);
        //获取当前时间
        //$nowtime=date("Y/m/d H:i:s",time());
        
        //需要把上传的图片删除
        $res_list = $DB->get_one("select * from essay where id= $id");
        $list  = explode("/",$res_list['pic']);
        
        $picName;
         foreach ( $list as $value){
            $picName = $value;
         }
         
         $filename = "../../upload/img/".$picName;
         //删除上传的图片操作
         if (file_exists($filename)) { 
            unlink($filename); 
          } 
                
        $DB->delete("essay","id =  {$id}");
   
           //$mysql->runSql("update class set status=0,edittime='$nowtime' where class_id=$class_id");    
       echo "<script>alert('操作成功！');location='essayList.php';</Script>";
       exit;    
    }  
    ?>
    
    <h1>文章列表</h1>
    <a href="../index.php" style="text-decoration:none;">查看主页效果</a>
    <a href="addEssay.php" style="text-decoration:none;">添加/修改文章</a>
    <table border="1">
            <tr bgcolor="gray"  height="100px" align="center"style="font-weight:bold;">
                <td>序号</td><td>标题</td><td>作者</td>
                <td>正文</td><td>添加时间</td><td>修改时间</td>
                <td>类型</td><td>微信转发标题</td><td>删除</td><td>修改</td>
            </tr>
            <?php

                if($res_list)
                {
                    $id = 0;
                    foreach($res_list as $value)
                    {
                        $addTime = date('Y-m-d',$value[addTime]);
                        if($value[endTime]!=0){
                            $endTime = date('Y-m-d',$value[endTime]);
                        }else{
                            $endTime = "未添加";
                        }
                        if($id%2){
                            echo "<tr bgcolor='#999' align='center' height='200px'>";
                          }else {
                              echo "<tr height='200px' align='center'>";
                            }
                            echo 
                             "<td width='100px'>$value[id]</td>
                              <td width='100px'>$value[title]</td>
                              <td width='100px'>$value[author]</td>
                              <td>$value[content]</td>
                              <td width='100px'>$addTime</td>
                              <td width='100px'>$endTime</td>
                              <td width='100px'>$value[type]</td>
                              <td width='100px'>$value[weixin_title]</td>

                              <td width='100px'><a href='essayList.php?action=del&id=$value[id]'>删除</a></td>
                              <td width='100px'><a href='addEssay.php?action=update&id=$value[id]'>修改</a></td>                                              
                              </tr>";
                              
                              $id++;
                    }
                }
                else
                {

                    echo "<tr><td colspan=4>无记录</td></tr>";
                }
            ?>



        </table> 
    <div style="text-align: center;font-size:30px;font-weight: bold;">
        <h1> <?php
            echo $multi;
        ?>
        </h1>
   </div>
      </body>
    </html>
